<?php
if( !defined('IN') ) die('bad request');
include_once( AROOT . 'controller'.DS.'app.class.php' );

class defaultController extends appController
{
	function __construct()
	{
		parent::__construct();
	}

	function index()
	{
		$data['title'] = $data['top_title'] = '首页';
		render( $data );
	}

	function login()
	{
		$data['title'] = $data['top_title'] = '登录';
		$data['js'] = array("login.js");
		render( $data );
	}
	
	function login2()
	{
		$data['title'] = $data['top_title'] = '登录';
		$data['css'] = array("login2/loginmain.css");
		render( $data );
	}

	function logout()
	{
		session_destroy();
		setcookie("name","",0);
		echo "<script> location='?a=index' </script>";
	}

	function register()
	{
		$data['title'] = $data['top_title'] = '注册';
		$data['js'] = array("register.js");
		render( $data );
	}

	function chkname()
	{
		if( v("name") )
		{
			if( selectone("name",v("name") ) )
				ajax_echo("has");
			else
				ajax_echo("ok");
		}
		else
		{
			ajax_echo("kidding");
		}
	}
	
	function chkemail()
	{
		if( v("email") )
		{
			if( selectone("email",v("email") ) )
				ajax_echo("has");
			else
				ajax_echo("ok");
		}
		else
		{
			ajax_echo("kidding");
		}
	}

	function chkuser()
	{
		if( v("name") && v("password") )
		{
			$res = selectone("name",v("name"));

			if( $res )
			{
				@extract($res);
				if( v("password")==$password )
				{
					if( $active )
					{
						ajax_echo("ok");
						$_SESSION["name"] = $name;
					}
					else
					{
						ajax_echo("noactive");
					}
				}
				else
				{
					ajax_echo("wrong");
				}
			}
			else
			{
				ajax_echo("no");
			}
		}
		else
		{
			ajax_echo("kidding");
		}
	}

	function adduser()
	{
		if( v("name") && v("password") )
		{
			@extract($_POST);

			$query = "insert into userInfo(name,password,email,phone,birthday,sex) values ( '$name', '$password', '$email', '$phone', '$birthday', '$sex')";
			if( !runSql($query) )
				ajax_echo("添加用户失败！");
			else
				ajax_echo("ok");
		}
		else
		{
			ajax_echo("kidding");
		}
	}

	function sendemail()
	{
		@extract($_REQUEST);

		$title = "SCUT Shop";
		$content =   "亲爱的".$name."点击以下链接激活您的账号\n"
				."http://scutshop.sinaapp.com/?a=activeuser&name=".urlencode($name)."&md5password=".md5($password);

		sendEmail( $email , $title , $content );
		return;
	}
	
	function forgetpassword()
	{
		if( v("email") )
		{
			$res = selectone( "email",v("email") );
			
			if( $res )
			{
				@extract($res);

				$title = "SCUT Shop";
				$content =   "亲爱的".$name.":\n"
				."您的用户名是".$name."，密码是".$password;

				sendEmail( $email , $title , $content );
			}
			else
			{
				echo "no";
			}
		}
		else
		{
			ajax_echo("链接错误");
		}
	}

	function activeuser()
	{
		@extract($_REQUEST);
		$res = selectone ("name", $name);
		
		if( $res )
		{
			@extract($res);
			if( md5( $password ) == $md5password )
			{
				if( $active )
				{
					info_page("您已经成功激活过您的账号了<br/><a href='?a=login'>点击登录</a>");
				}
				else
				{
					update($name,"active",true);
					info_page("恭喜".$name."，您的账号激活成功~<br/><a href='?a=login'>点击登录</a>");
				}
			}
			else
			{
				info_page( "链接错误！" );
			}
		}
		else
		{
			info_page("无此账号！");
		}
	}

	function info(){
		if( s("name") )
		{
			$res = selectone("name",s("name"));
			@extract($res);
			$str = "邮箱：".$email."<br/>生日：".substr($birthday,0,4)."年".substr($birthday,4,2)."月".substr($birthday,6,2)."日"."<br/>短号：".$phone."<br/>";
			info_page( $str ,$name."的信息");
		}
		else
		{
			info_page("请登录");
		}
	}
	
	function showinfo()
	{
		if( v("info") )
		{
			$title = v("title")?v("title"):"提示信息";
			info_page( v("info") ,$title);
		}
		else
		{
			info_page("nothing to say……");
		}
	}
	
	function uploadimage()
	{
		if( !g("name") )
			return info_page( "请登录……" );
		$data['title'] = $data['top_title'] = '上传照片';
		$data['js'] = array("myjsp.js","uploadImage.js");
		$data['css'] = array("uploadImage.css");
		render( $data );
	}
	
	function processupload()
	{
		if( !g("name") )
		{
			echo "no";
			return;
		}
		
		$s = new SaeStorage();
		
		$name = g("name");
		$fileName = g("name").date('Y-m-d H:i:s').$_FILES['upload']['name'];
		$resName = $_FILES['upload']['tmp_name'];

		$url = $s->upload('photo',$fileName,$resName);
		if( $url )
		{
			$query = "insert into image values ('$fileName','$name','$url')";
			runSql( $query );
			echo "ok";
		}
	}
	
	function listFile()
	{
		$domain = "photo";
		$s = new SaeStorage();
		
		$start = 0;
		$num = 0;
		while($list = $s->getList($domain,NULL,25,$start))
		{
			$start += count($list);
			foreach($list as $file)
			{
				$url = $s->getUrl($domain,$file);
				$type = substr($url,-3,3);
				echo $url."<br/>";
			}
		}
	}
	
	function showimage()
	{
		$data['title'] = $data['top_title'] = '查看照片';
		$data['js'] = array("listfile.js");
		$data["photo"] = array();
		
		$res = select("name",g("name"),"image");
		
		if( $res) foreach($res as $one)
		{
			$url = $one['url'];
			$type = strtolower( substr($url,-3,3) );
			if( $type=="jpg"
			||	$type=="jpeg"
			||	$type=="png"
			||	$type=="bmp"
			||	$type=="gif" )
			{
				$data["photo"][$num++] = $url;
			}
		}
		$data["num"] = $num;
		render( $data );
	}
	
	function test()
	{
		$data['title'] = $data['top_title'] = '测试';
		$data['js'] = array("test.js");
		$data['test'] = $_COOKIE["name"];
		render( $data );
	}

	function about()
	{
		return info_page( "SCUT 二男三女 荣誉出品" , "About" );
	}

	function contact()
	{
		return info_page( "SCUT - 华南理工大学南校区 |  11软件3班" , "Follow Me" );
	}
	
	function createtable()
	{
		echo runSql("drop table image");
		$query = "create table image ( filename varchar(50) binary primary key, name varchar(50) binary not null, url varchar(100) binary,FOREIGN KEY(name) REFERENCES userInfo on delete cascade) ENGINE=MyISAM DEFAULT CHARSET=utf8;";
		echo runSql( $query );
		global $db;
		echo $db->error;
	}
	
	function testPost()
	{
		print_r($_POST);
	}
}
